﻿using System;
using System.Linq;
using System.Web.UI;
using Coolite.Ext.Web;
using Pap.Web.Modules.Loan.ViewModel;
using Pap.Web.Utils;
using Utils.UIPermission;

namespace Pap.Web.Modules.Loan.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class ExpertWaitingList : BasePage<ExpertWaitingListViewModel>, IPermittedPage
    {
        #region Event Handlers
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && !Ext.IsAjaxRequest)
            {

            }
        }

        /// <summary>
        /// Handles the Clicked event of the btnBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void btnBack_Clicked(object sender, AjaxEventArgs args)
        {
            this.RedirectToPreviousPage();
        }

        /// <summary>
        /// Handles the Clicked event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void btnSave_Clicked(object sender, AjaxEventArgs args)
        {
            string strJSON = args.ExtraParams["GridData"];
            Newtonsoft.Json.Linq.JArray jarrChanges = (Newtonsoft.Json.Linq.JArray)JSON.Deserialize(strJSON);
            var changedLoans = (from item in jarrChanges
                                where item["DecisionID"].ToString() != "None"
                                select new
                                {
                                    LoanID = item["LoanID"].ToString().Trim('"').ToGuid(),
                                    Notes = item["Notes"].ToString().Trim('"'),
                                    Decision = (Pap.Services.WorkflowEngine.ActionTypes)Enum.Parse(typeof(Pap.Services.WorkflowEngine.ActionTypes), item["DecisionID"].ToString().Trim('"')),
                                }).ToArray();

            foreach (var loan in changedLoans)
            {
                this.ViewModel.TakeDecisionForLoan(loan.LoanID, loan.Decision, loan.Notes);
            }
        }
        #endregion

        #region IPermittedPage Members

        /// <summary>
        /// Gets the name of the permission.
        /// </summary>
        /// <value>
        /// The name of the permission.
        /// </value>
        public string PermissionName
        {
            get { return "ExpertWaitingList"; }
        }

        #endregion
    }
}
